package com.hyg.lib_common.DataModel.QuestionNaire;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class NinePhysiqueScore implements Parcelable {

    private String PhysiqueName = "";           // 体质名称
    private float PhysiqueScore = 0f;          // 体质总得分
    private float PhysiqueTrans = 0f;          // 体质转化分
    private ArrayList<Integer> PhysiqueResult = new ArrayList<Integer>();   // 体质各题得分过程；

    public NinePhysiqueScore(String PhysiqueName, float PhysiqueScore, float PhysiqueTrans, ArrayList<Integer> PhysiqueResult){
        this.PhysiqueName = PhysiqueName;
        this.PhysiqueScore = PhysiqueScore;
        this.PhysiqueTrans = PhysiqueTrans;
        PhysiqueResult.clear();
        this.PhysiqueResult.addAll(PhysiqueResult);
    }

    public NinePhysiqueScore(String PhysiqueName, ArrayList<Integer> PhysiqueResult){
        this.PhysiqueName = PhysiqueName;
        this.PhysiqueResult.clear();
        this.PhysiqueResult.addAll(PhysiqueResult);
        // 直接计算 出体质得分 与 体质转化分；
        this.PhysiqueTrans = MeasureTrans(MeasureScore(),this.PhysiqueResult.size());
        Log.d("PhysiqueTrans",PhysiqueTrans+"");
    }
    public NinePhysiqueScore(String PhysiqueName){
        this.PhysiqueName = PhysiqueName;
    }

    public JSONObject output_json(){
        JSONObject data = new JSONObject();
        try {
            data.put("name",PhysiqueName);
            JSONArray ja = new JSONArray();
            for (int i = 0; i < PhysiqueResult.size(); i++){
                ja.put(PhysiqueResult.get(i));
            }
            data.put("result",ja);
            data.put("score",PhysiqueScore);
            data.put("trans",PhysiqueTrans);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return data;

        //[{"name":"平和质","result":[3,4,3,2,2],"score":14,"trans":45},
        // {"name":"气虚质","result":[2,3,5,4],"score":14,"trans":62.5},
        // {"name":"阳虚质","result":[4,3,3,2],"score":12,"trans":50},
        // {"name":"阴虚质","result":[1,2,1,3],"score":7,"trans":18.75},
        // {"name":"痰湿质","result":[3,4,4,5],"score":16,"trans":75},
        // {"name":"湿热质","result":[5,4,4,3],"score":16,"trans":75},
        // {"name":"血瘀质","result":[2,2,1,2],"score":7,"trans":18.75},
        // {"name":"气郁质","result":[4,2,2,1],"score":9,"trans":31.25},
        // {"name":"特禀质","result":[4,1,4,1],"score":10,"trans":37.5}]
    }

    public float MeasureScore(){
        PhysiqueScore = 0f;
        for (int i = 0; i < PhysiqueResult.size(); i++){
            PhysiqueScore += PhysiqueResult.get(i);
        }
        Log.d("PhysiqueScore",PhysiqueScore+"");
        return PhysiqueScore;
    }

    //计算 转化分方式
    public float MeasureTrans(float scroe,int itemNumber){
        Log.d("TiZhiScroe","scroe:"+scroe+"  itemNumber:"+itemNumber);
        float changeScroe = ((scroe-itemNumber)*100/(itemNumber*4));
        Log.d("TiZhiScroe",changeScroe+"");
        return (float)(Math.round(changeScroe*100)/100);
    }

    protected NinePhysiqueScore(Parcel in) {
        PhysiqueName = in.readString();
        PhysiqueScore = in.readFloat();
        PhysiqueTrans = in.readFloat();
    }

    public static final Creator<NinePhysiqueScore> CREATOR = new Creator<NinePhysiqueScore>() {
        @Override
        public NinePhysiqueScore createFromParcel(Parcel in) {
            return new NinePhysiqueScore(in);
        }

        @Override
        public NinePhysiqueScore[] newArray(int size) {
            return new NinePhysiqueScore[size];
        }
    };

    public String getPhysiqueName() {
        return PhysiqueName;
    }

    public void setPhysiqueName(String physiqueName) {
        PhysiqueName = physiqueName;
    }

    public float getPhysiqueScore() {
        return PhysiqueScore;
    }

    public void setPhysiqueScore(float physiqueScore) {
        PhysiqueScore = physiqueScore;
    }

    public float getPhysiqueTrans() {
        return PhysiqueTrans;
    }

    public void setPhysiqueTrans(float physiqueTrans) {
        PhysiqueTrans = physiqueTrans;
    }

    public ArrayList<Integer> getPhysiqueResult() {
        return PhysiqueResult;
    }

    public void setPhysiqueResult(ArrayList<Integer> physiqueResult) {
        this.PhysiqueResult.clear();
        this.PhysiqueResult.addAll(physiqueResult);
        // 顺便直接计算 出体质得分 与 体质转化分；
        this.PhysiqueTrans = MeasureTrans(MeasureScore(),PhysiqueResult.size());
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(PhysiqueName);
        dest.writeFloat(PhysiqueScore);
        dest.writeFloat(PhysiqueTrans);
    }
}
